

import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.channel.*;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.handler.logging.LogLevel;
import io.netty.handler.logging.LoggingHandler;
import lombok.extern.slf4j.Slf4j;

/**
 * @author zfChen
 * @create 2022/7/5 11:00
 */
@Slf4j
public class NettyServer01 {
    public static void main(String[] args) throws InterruptedException {
        new ServerBootstrap()
                .group(new NioEventLoopGroup())
                .channel(NioServerSocketChannel.class)
                .childHandler(new ChannelInitializer<NioSocketChannel>() {
                    protected void initChannel(NioSocketChannel ch) {
                        // 通过channel拿到pipeline
                        ch.pipeline().addLast("h1", new ChannelInboundHandlerAdapter() {  //调试方便，添加处理器名称h1
                            @Override
                            public void channelRead(ChannelHandlerContext ctx, Object msg) {
                                log.info("1");
                                ctx.fireChannelRead(msg); // 1
                            }
                        });
                        ch.pipeline().addLast("h2", new ChannelInboundHandlerAdapter() {
                            @Override
                            public void channelRead(ChannelHandlerContext ctx, Object msg) {
                                log.info("2");
                                ctx.fireChannelRead(msg); // 2
                            }
                        });
                        ch.pipeline().addLast("h3", new ChannelInboundHandlerAdapter() {
                            @Override
                            public void channelRead(ChannelHandlerContext ctx, Object msg) {
                                log.info("3");
                                ctx.channel().write(msg); // 3
                            }
                        });
                        ch.pipeline().addLast("h4", new ChannelOutboundHandlerAdapter() {
                            @Override
                            public void write(ChannelHandlerContext ctx, Object msg,
                                              ChannelPromise promise) {
                                log.info("4");
                                ctx.write(msg, promise); // 4	//写入数据才会触发出站处理器
                            }
                        });
                        ch.pipeline().addLast("h5", new ChannelOutboundHandlerAdapter() {
                            @Override
                            public void write(ChannelHandlerContext ctx, Object msg,
                                              ChannelPromise promise) {
                                log.info("5");
                                ctx.write(msg, promise); // 5
                            }
                        });
                        ch.pipeline().addLast("h6", new ChannelOutboundHandlerAdapter() {
                            @Override
                            public void write(ChannelHandlerContext ctx, Object msg,
                                              ChannelPromise promise) {
                                log.info("6");
                                ctx.write(msg, promise); // 6
                            }
                        });
                    }
                })
                .bind(8801);
    }
}
